﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CalendarApplication
{
    public partial class frmDayDetails : Form
    {
        ucCalendarContainer _cal;
        DateTime _d;
        public frmDayDetails(DateTime d, ucCalendarContainer Cal)
        {
            InitializeComponent();
            _cal = Cal;
            _d = d;

            //initializing datetimepicker to these values. Not sure this is appropriate
            dtpSTime.Value = DateTime.Parse("6:00:00 AM");
            dtpETime.Value = DateTime.Parse("7:00:00 AM");


            //opening connection
            //TODO this should be moved to Service Layer probably
            SqlConnection conn = new SqlConnection(Utilities.DBConnection);
            conn.Open();
            SqlCommand sc = new SqlCommand("SELECT * FROM tblCalendarMain WHERE Date='" + d + "'", conn);
            sc.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(sc);
            DataTable ds = new DataTable();
            da.SelectCommand = sc;
            da.Fill(ds);
            dataGridView1.DataSource = ds;
            dataGridView1.Columns["pkID"].Visible = false;
            //dataGridView1.Columns["Date"].Visible = false;//dataGridView1.Columns["EndTime"].Visible = false;
            dtpDate.Value = d;
            conn.Close();
            conn.Dispose();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Attempting straight cell edit below removed, well, because it sucked
        //DISABLING CELL EDIT BECAUSE IT SUCKS
        //REALLY
        //private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        //{
        //    try
        //    {
        //        string pkID = dataGridView1.SelectedRows[0].Cells["pkID"].Value.ToString();
        //        int _pkID = int.Parse(pkID);
        //        string _date = dataGridView1.SelectedRows[0].Cells["Date"].Value.ToString();
        //        string _starttime = dataGridView1.SelectedRows[0].Cells["StartTime"].Value.ToString();
        //        string _endtime = dataGridView1.SelectedRows[0].Cells["EndTime"].Value.ToString();
        //        string _description = dataGridView1.SelectedRows[0].Cells["Description"].Value.ToString();

        //        //EnterAppointmentDayForm(_pkID, _date, _starttime, _endtime, _description);
        //        //DateTime dt;            

        //        SqlConnection conn = new SqlConnection(Utilities.DBConnection);
        //        conn.Open();

        //        SqlCommand sc = new SqlCommand("UPDATE tblCalendarMain SET Date = @Date, StartTime = @StartTime, EndTime = @Endtime, Description = @Description WHERE pkID = '" + _pkID + "'", conn);
        //        ////sc.CommandType = CommandType.Text;
        //        ////sc.CommandText = ("UPDATE tblCalendarMain SET Date = @Date, StartTime = @StartTime, EndTime = @Endtime, Description = @Description WHERE pkID = '"+_pkID+"'", conn); 

        //        sc.Parameters.AddWithValue("@pkID", _pkID);
        //        sc.Parameters.AddWithValue("@Date", _date);
        //        sc.Parameters.AddWithValue("@StartTime", _starttime);
        //        sc.Parameters.AddWithValue("@EndTime", _endtime);
        //        sc.Parameters.AddWithValue("@Description", _description);
        //        sc.ExecuteNonQuery();
        //        _cal.PaintNewPanel();
        //        conn.Close();
        //        Dispose();
        //        //Form1 f = new Form1();
        //        //f.Show();
        //    }
        //    catch 
        //    {
        //        MessageBox.Show("Error WILL ROBINSON","DANGER");
        //    }
        //}


        //Entering appointment into database
        //TODO Move into service layer, Overload for different parameters, capture "user" for ownership, capture "client" data as well
        //This was written a long time ago TODO update to current requirements

        private void EnterAppointmentDayForm(string date, string starttime, string endtime, string description)
        {
            SqlConnection conn = new SqlConnection(Utilities.DBConnection);
            conn.Open();

            //TODO move this into stored procedures
            SqlCommand sc = new SqlCommand("INSERT INTO tblCalendarMain ([Date], [StartTime], [EndTime], [Description])" +
            "VALUES (@Date, @StartTime, @Endtime, @Description)", conn);
            sc.Parameters.AddWithValue("@Date", date);
            sc.Parameters.AddWithValue("@StartTime", starttime);
            sc.Parameters.AddWithValue("@EndTime", endtime);
            sc.Parameters.AddWithValue("@Description", description);
            sc.ExecuteNonQuery();
            _cal.PaintNewPanel();
            conn.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //make sure appointment is good before committing
            //TODO Move entire procedure into service layer
            if (MessageBox.Show("Are You Sure You Want To Commit This Appointment To Your Calendar?", "Confirm" +
                " Commit", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                string date;
                string starttime;
                string endtime;
                string description;

                date = dtpDate.Value.ToString();
                starttime = dtpSTime.Value.ToString();
                endtime = dtpETime.Value.ToString();
                description = txtDesc.Text;

                EnterAppointmentDayForm(date, starttime, endtime, description);

                this.dataGridView1.Invalidate();
                refillGrid(_d);
            }
            else
            {
                MessageBox.Show("Nothing Was Committed");
            }
        }

        //simply updates UI when appointment added
        private void refillGrid(DateTime d)
        {

            //TODO abstract connection routine. Used all over should be own class
            SqlConnection conn = new SqlConnection(Utilities.DBConnection);
            conn.Open();

            //TODO move into stored procedure
            SqlCommand sc = new SqlCommand("SELECT * FROM tblCalendarMain WHERE Date='" + d + "'", conn);
            sc.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(sc);
            DataTable ds = new DataTable();
            da.SelectCommand = sc;
            da.Fill(ds);
            dataGridView1.DataSource = ds;

            //I don't even...
            //dataGridView1.Columns["pkID"].Visible = false;
            //dataGridView1.Columns["Date"].Visible = false;
            //dataGridView1.Columns["EndTime"].Visible = false;
            //dtpDate.Value = d;
            conn.Close();
            conn.Dispose();
        }
    }
}
